{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Fake News Prediction.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyPnSGKbHAihJouuu4PqFnfQ",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/SarraChebaane/Machine-Learning-Projects-for-Beginners/blob/main/Fake_News_Prediction.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "rmZ9x91aiYVW"
      },
      "outputs": [],
      "source": [
        "import numpy as np \n",
        "import pandas as pd\n",
        "# This library is very useful for searching the text in a document \n",
        "import re\n",
        "\n",
        "# Remove words which doen't add much value to a paragraph \n",
        "from nltk.corpus import stopwords\n",
        "\n",
        "# This function take a word and removes the prefix and suffix of that word and returns the root word of it \n",
        "from nltk.stem.porter import PorterStemmer \n",
        "\n",
        "# This function convert the text into feature vectors \n",
        "from sklearn.feature_extraction.text import TfidfVectorizer\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.linear_model import LogisticRegression \n",
        "from sklearn.metrics import accuracy_score"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import nltk \n",
        "nltk.download('stopwords')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uq4eVGg0jMF3",
        "outputId": "a2b3e053-097f-44b5-93f0-8667ac9805c2"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "[nltk_data] Downloading package stopwords to /root/nltk_data...\n",
            "[nltk_data]   Unzipping corpora/stopwords.zip.\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# printing the stopwords in English\n",
        "print(stopwords.words('english'))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "z1QLAkuQoUna",
        "outputId": "b8aba541-f3b9-4346-8486-03f2a0424c9b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', \"you're\", \"you've\", \"you'll\", \"you'd\", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', \"she's\", 'her', 'hers', 'herself', 'it', \"it's\", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', \"that'll\", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', \"don't\", 'should', \"should've\", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', \"aren't\", 'couldn', \"couldn't\", 'didn', \"didn't\", 'doesn', \"doesn't\", 'hadn', \"hadn't\", 'hasn', \"hasn't\", 'haven', \"haven't\", 'isn', \"isn't\", 'ma', 'mightn', \"mightn't\", 'mustn', \"mustn't\", 'needn', \"needn't\", 'shan', \"shan't\", 'shouldn', \"shouldn't\", 'wasn', \"wasn't\", 'weren', \"weren't\", 'won', \"won't\", 'wouldn', \"wouldn't\"]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Data Pre-Processing"
      ],
      "metadata": {
        "id": "BhxOWNIVouED"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# loading the dataset to a pandas DataFrame \n",
        "news_dataset = pd.read_csv('/content/train.csv')"
      ],
      "metadata": {
        "id": "5FXv9qw7ow24"
      },
      "execution_count": 11,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "news_dataset.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GueD5D9DrFuA",
        "outputId": "4242ff34-9d21-4f89-d589-2c48f167a934"
      },
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(20800, 5)"
            ]
          },
          "metadata": {},
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# print the first 5 rows of the dataframe\n",
        "news_dataset.head()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 354
        },
        "id": "6tDyPumArKVc",
        "outputId": "5043b52a-1e26-4a23-ab8c-e108f72e9e9b"
      },
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "   id                                              title              author  \\\n",
              "0   0  House Dem Aide: We Didn’t Even See Comey’s Let...       Darrell Lucus   \n",
              "1   1  FLYNN: Hillary Clinton, Big Woman on Campus - ...     Daniel J. Flynn   \n",
              "2   2                  Why the Truth Might Get You Fired  Consortiumnews.com   \n",
              "3   3  15 Civilians Killed In Single US Airstrike Hav...     Jessica Purkiss   \n",
              "4   4  Iranian woman jailed for fictional unpublished...      Howard Portnoy   \n",
              "\n",
              "                                                text  label  \n",
              "0  House Dem Aide: We Didn’t Even See Comey’s Let...      1  \n",
              "1  Ever get the feeling your life circles the rou...      0  \n",
              "2  Why the Truth Might Get You Fired October 29, ...      1  \n",
              "3  Videos 15 Civilians Killed In Single US Airstr...      1  \n",
              "4  Print \\nAn Iranian woman has been sentenced to...      1  "
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-df838668-de34-4f12-9cea-393d02f909df\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>id</th>\n",
              "      <th>title</th>\n",
              "      <th>author</th>\n",
              "      <th>text</th>\n",
              "      <th>label</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0</td>\n",
              "      <td>House Dem Aide: We Didn’t Even See Comey’s Let...</td>\n",
              "      <td>Darrell Lucus</td>\n",
              "      <td>House Dem Aide: We Didn’t Even See Comey’s Let...</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>FLYNN: Hillary Clinton, Big Woman on Campus - ...</td>\n",
              "      <td>Daniel J. Flynn</td>\n",
              "      <td>Ever get the feeling your life circles the rou...</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>2</td>\n",
              "      <td>Why the Truth Might Get You Fired</td>\n",
              "      <td>Consortiumnews.com</td>\n",
              "      <td>Why the Truth Might Get You Fired October 29, ...</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>3</td>\n",
              "      <td>15 Civilians Killed In Single US Airstrike Hav...</td>\n",
              "      <td>Jessica Purkiss</td>\n",
              "      <td>Videos 15 Civilians Killed In Single US Airstr...</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>4</td>\n",
              "      <td>Iranian woman jailed for fictional unpublished...</td>\n",
              "      <td>Howard Portnoy</td>\n",
              "      <td>Print \\nAn Iranian woman has been sentenced to...</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-df838668-de34-4f12-9cea-393d02f909df')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-df838668-de34-4f12-9cea-393d02f909df button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-df838668-de34-4f12-9cea-393d02f909df');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# counting the number of missing values in the dataset\n",
        "news_dataset.isnull().sum()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bxJ6sHX8yaDF",
        "outputId": "a001b43f-5ae0-4f33-99e6-06947dab81c3"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "id           0\n",
              "title      558\n",
              "author    1957\n",
              "text        39\n",
              "label        0\n",
              "dtype: int64"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# replacing the null values with empty string \n",
        "news_dataset = news_dataset.fillna('')"
      ],
      "metadata": {
        "id": "OdRF7KKczUTY"
      },
      "execution_count": 17,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# merging the author name and news title \n",
        "news_dataset['content'] = news_dataset['author'] + ' '+ news_dataset['title']"
      ],
      "metadata": {
        "id": "MprZavJ14HOf"
      },
      "execution_count": 18,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(news_dataset['content'])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wZM6Vi0V4oHN",
        "outputId": "b54d678a-dca3-4a81-a69b-6b4b79f178ba"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "0        Darrell Lucus House Dem Aide: We Didn’t Even S...\n",
            "1        Daniel J. Flynn FLYNN: Hillary Clinton, Big Wo...\n",
            "2        Consortiumnews.com Why the Truth Might Get You...\n",
            "3        Jessica Purkiss 15 Civilians Killed In Single ...\n",
            "4        Howard Portnoy Iranian woman jailed for fictio...\n",
            "                               ...                        \n",
            "20795    Jerome Hudson Rapper T.I.: Trump a ’Poster Chi...\n",
            "20796    Benjamin Hoffman N.F.L. Playoffs: Schedule, Ma...\n",
            "20797    Michael J. de la Merced and Rachel Abrams Macy...\n",
            "20798    Alex Ansary NATO, Russia To Hold Parallel Exer...\n",
            "20799              David Swanson What Keeps the F-35 Alive\n",
            "Name: content, Length: 20800, dtype: object\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# separating the data & label \n",
        "X = news_dataset.drop(columns = 'label', axis = 1)\n",
        "Y = news_dataset['label']"
      ],
      "metadata": {
        "id": "ZETDKxXh4xfg"
      },
      "execution_count": 20,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(X)\n",
        "print(Y)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "T4Hj8q-w5Mt3",
        "outputId": "6bd7784f-33ae-4af4-bf1e-14518d120f42"
      },
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "          id                                              title  \\\n",
            "0          0  House Dem Aide: We Didn’t Even See Comey’s Let...   \n",
            "1          1  FLYNN: Hillary Clinton, Big Woman on Campus - ...   \n",
            "2          2                  Why the Truth Might Get You Fired   \n",
            "3          3  15 Civilians Killed In Single US Airstrike Hav...   \n",
            "4          4  Iranian woman jailed for fictional unpublished...   \n",
            "...      ...                                                ...   \n",
            "20795  20795  Rapper T.I.: Trump a ’Poster Child For White S...   \n",
            "20796  20796  N.F.L. Playoffs: Schedule, Matchups and Odds -...   \n",
            "20797  20797  Macy’s Is Said to Receive Takeover Approach by...   \n",
            "20798  20798  NATO, Russia To Hold Parallel Exercises In Bal...   \n",
            "20799  20799                          What Keeps the F-35 Alive   \n",
            "\n",
            "                                          author  \\\n",
            "0                                  Darrell Lucus   \n",
            "1                                Daniel J. Flynn   \n",
            "2                             Consortiumnews.com   \n",
            "3                                Jessica Purkiss   \n",
            "4                                 Howard Portnoy   \n",
            "...                                          ...   \n",
            "20795                              Jerome Hudson   \n",
            "20796                           Benjamin Hoffman   \n",
            "20797  Michael J. de la Merced and Rachel Abrams   \n",
            "20798                                Alex Ansary   \n",
            "20799                              David Swanson   \n",
            "\n",
            "                                                    text  \\\n",
            "0      House Dem Aide: We Didn’t Even See Comey’s Let...   \n",
            "1      Ever get the feeling your life circles the rou...   \n",
            "2      Why the Truth Might Get You Fired October 29, ...   \n",
            "3      Videos 15 Civilians Killed In Single US Airstr...   \n",
            "4      Print \\nAn Iranian woman has been sentenced to...   \n",
            "...                                                  ...   \n",
            "20795  Rapper T. I. unloaded on black celebrities who...   \n",
            "20796  When the Green Bay Packers lost to the Washing...   \n",
            "20797  The Macy’s of today grew from the union of sev...   \n",
            "20798  NATO, Russia To Hold Parallel Exercises In Bal...   \n",
            "20799    David Swanson is an author, activist, journa...   \n",
            "\n",
            "                                                 content  \n",
            "0      Darrell Lucus House Dem Aide: We Didn’t Even S...  \n",
            "1      Daniel J. Flynn FLYNN: Hillary Clinton, Big Wo...  \n",
            "2      Consortiumnews.com Why the Truth Might Get You...  \n",
            "3      Jessica Purkiss 15 Civilians Killed In Single ...  \n",
            "4      Howard Portnoy Iranian woman jailed for fictio...  \n",
            "...                                                  ...  \n",
            "20795  Jerome Hudson Rapper T.I.: Trump a ’Poster Chi...  \n",
            "20796  Benjamin Hoffman N.F.L. Playoffs: Schedule, Ma...  \n",
            "20797  Michael J. de la Merced and Rachel Abrams Macy...  \n",
            "20798  Alex Ansary NATO, Russia To Hold Parallel Exer...  \n",
            "20799            David Swanson What Keeps the F-35 Alive  \n",
            "\n",
            "[20800 rows x 5 columns]\n",
            "0        1\n",
            "1        0\n",
            "2        1\n",
            "3        1\n",
            "4        1\n",
            "        ..\n",
            "20795    0\n",
            "20796    0\n",
            "20797    0\n",
            "20798    1\n",
            "20799    1\n",
            "Name: label, Length: 20800, dtype: int64\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Stemming \n",
        "\n",
        "Stemming is the process of reducing a word to its Roots word\n",
        "\n",
        "exemple : actor , actress , acting --> act"
      ],
      "metadata": {
        "id": "xHXRBfLG5d7E"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "port_stem = PorterStemmer()"
      ],
      "metadata": {
        "id": "OVFXUl0h6Kfg"
      },
      "execution_count": 22,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def stemming(content):\n",
        "  stemmed_content = re.sub('[^a-zA-Z]',' ',content)\n",
        "  # convert all the alphabet to lowercase alphabet\n",
        "  stemmed_content = stemmed_content.lower()\n",
        "  # split the dataset into list\n",
        "  stemmed_content = stemmed_content.split()\n",
        "  stemmed_content = [port_stem.stem(word) for word in stemmed_content if not word in stopwords.words('english') ]\n",
        "  stemmed_content = ' '.join(stemmed_content)\n",
        "  return stemmed_content"
      ],
      "metadata": {
        "id": "CUK96tpu6aYQ"
      },
      "execution_count": 23,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "news_dataset['content'] = news_dataset['content'].apply(stemming)"
      ],
      "metadata": {
        "id": "s5MRpeoXBbnp"
      },
      "execution_count": 24,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(news_dataset['content'])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "I1Z1GwZgBzKi",
        "outputId": "c85a05f4-7668-4bdb-f722-a3336f0b9bd2"
      },
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "0        darrel lucu hous dem aid even see comey letter...\n",
            "1        daniel j flynn flynn hillari clinton big woman...\n",
            "2                   consortiumnew com truth might get fire\n",
            "3        jessica purkiss civilian kill singl us airstri...\n",
            "4        howard portnoy iranian woman jail fiction unpu...\n",
            "                               ...                        \n",
            "20795    jerom hudson rapper trump poster child white s...\n",
            "20796    benjamin hoffman n f l playoff schedul matchup...\n",
            "20797    michael j de la merc rachel abram maci said re...\n",
            "20798    alex ansari nato russia hold parallel exercis ...\n",
            "20799                            david swanson keep f aliv\n",
            "Name: content, Length: 20800, dtype: object\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# separating the data and label\n",
        "X = news_dataset['content'].values\n",
        "Y = news_dataset['label'].values\n"
      ],
      "metadata": {
        "id": "wBNS5JvABzZm"
      },
      "execution_count": 27,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(X)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "d4iFgkjGCVTg",
        "outputId": "5c3a3c5e-dc69-4663-ab5b-052d4a89ae2c"
      },
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "['darrel lucu hous dem aid even see comey letter jason chaffetz tweet'\n",
            " 'daniel j flynn flynn hillari clinton big woman campu breitbart'\n",
            " 'consortiumnew com truth might get fire' ...\n",
            " 'michael j de la merc rachel abram maci said receiv takeov approach hudson bay new york time'\n",
            " 'alex ansari nato russia hold parallel exercis balkan'\n",
            " 'david swanson keep f aliv']\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(Y)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HUd795RqCZct",
        "outputId": "b7529053-4fcc-4242-ee7e-31154137a3b9"
      },
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[1 0 1 ... 0 1 1]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "Y.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "h5fNLkRdCcBD",
        "outputId": "2d371faa-bcdf-4001-eb9f-32e3196f2767"
      },
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(20800,)"
            ]
          },
          "metadata": {},
          "execution_count": 32
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# converting the text data into numerical data \n",
        "vectorizer = TfidfVectorizer()\n",
        "vectorizer.fit(X)\n",
        "\n",
        "X = vectorizer.transform(X)"
      ],
      "metadata": {
        "id": "4BHXlj-MCnNf"
      },
      "execution_count": 33,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(X)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "s4N4s1hoDVzw",
        "outputId": "e384501c-60b3-4c95-cd6a-7be4afc42d53"
      },
      "execution_count": 34,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "  (0, 15686)\t0.28485063562728646\n",
            "  (0, 13473)\t0.2565896679337957\n",
            "  (0, 8909)\t0.3635963806326075\n",
            "  (0, 8630)\t0.29212514087043684\n",
            "  (0, 7692)\t0.24785219520671603\n",
            "  (0, 7005)\t0.21874169089359144\n",
            "  (0, 4973)\t0.233316966909351\n",
            "  (0, 3792)\t0.2705332480845492\n",
            "  (0, 3600)\t0.3598939188262559\n",
            "  (0, 2959)\t0.2468450128533713\n",
            "  (0, 2483)\t0.3676519686797209\n",
            "  (0, 267)\t0.27010124977708766\n",
            "  (1, 16799)\t0.30071745655510157\n",
            "  (1, 6816)\t0.1904660198296849\n",
            "  (1, 5503)\t0.7143299355715573\n",
            "  (1, 3568)\t0.26373768806048464\n",
            "  (1, 2813)\t0.19094574062359204\n",
            "  (1, 2223)\t0.3827320386859759\n",
            "  (1, 1894)\t0.15521974226349364\n",
            "  (1, 1497)\t0.2939891562094648\n",
            "  (2, 15611)\t0.41544962664721613\n",
            "  (2, 9620)\t0.49351492943649944\n",
            "  (2, 5968)\t0.3474613386728292\n",
            "  (2, 5389)\t0.3866530551182615\n",
            "  (2, 3103)\t0.46097489583229645\n",
            "  :\t:\n",
            "  (20797, 13122)\t0.2482526352197606\n",
            "  (20797, 12344)\t0.27263457663336677\n",
            "  (20797, 12138)\t0.24778257724396507\n",
            "  (20797, 10306)\t0.08038079000566466\n",
            "  (20797, 9588)\t0.174553480255222\n",
            "  (20797, 9518)\t0.2954204003420313\n",
            "  (20797, 8988)\t0.36160868928090795\n",
            "  (20797, 8364)\t0.22322585870464118\n",
            "  (20797, 7042)\t0.21799048897828688\n",
            "  (20797, 3643)\t0.21155500613623743\n",
            "  (20797, 1287)\t0.33538056804139865\n",
            "  (20797, 699)\t0.30685846079762347\n",
            "  (20797, 43)\t0.29710241860700626\n",
            "  (20798, 13046)\t0.22363267488270608\n",
            "  (20798, 11052)\t0.4460515589182236\n",
            "  (20798, 10177)\t0.3192496370187028\n",
            "  (20798, 6889)\t0.32496285694299426\n",
            "  (20798, 5032)\t0.4083701450239529\n",
            "  (20798, 1125)\t0.4460515589182236\n",
            "  (20798, 588)\t0.3112141524638974\n",
            "  (20798, 350)\t0.28446937819072576\n",
            "  (20799, 14852)\t0.5677577267055112\n",
            "  (20799, 8036)\t0.45983893273780013\n",
            "  (20799, 3623)\t0.37927626273066584\n",
            "  (20799, 377)\t0.5677577267055112\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Splitting the dataset to training & test data"
      ],
      "metadata": {
        "id": "6NHON1SgDdji"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X_train, X_test, Y_train,Y_test = train_test_split(X,Y,test_size=0.2,stratify=Y,random_state=2)"
      ],
      "metadata": {
        "id": "-eByyneWDcK7"
      },
      "execution_count": 43,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Training the Model : Logistic Regression"
      ],
      "metadata": {
        "id": "NdCWnUqeEN6t"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model = LogisticRegression()"
      ],
      "metadata": {
        "id": "cAgF57xPEHyt"
      },
      "execution_count": 36,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "model.fit(X_train,Y_train)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8sRaxLkAEah3",
        "outputId": "2263d1af-dacb-4924-be54-c4b0d3959013"
      },
      "execution_count": 39,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "LogisticRegression()"
            ]
          },
          "metadata": {},
          "execution_count": 39
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Evaluation\n"
      ],
      "metadata": {
        "id": "bNMeseQTEmfH"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# accuracy sccore on the training data \n",
        "X_train_prediction = model.predict(X_train)\n",
        "training_data_accuracy = accuracy_score(X_train_prediction, Y_train)"
      ],
      "metadata": {
        "id": "c8bkYDX5EoCa"
      },
      "execution_count": 40,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print('Accuracy score of the training data : ', training_data_accuracy)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JRomR8JwFTKK",
        "outputId": "8aca1111-18ac-4a77-9fbe-2311a63e1278"
      },
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Accuracy score of the training data :  0.9865985576923076\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# accuracy sccore on the test data \n",
        "X_test_prediction = model.predict(X_test)\n",
        "test_data_accuracy = accuracy_score(X_test_prediction, Y_test)"
      ],
      "metadata": {
        "id": "9UJ74to9Ffj6"
      },
      "execution_count": 44,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print('Accuracy score of the test data : ', test_data_accuracy)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-I87y7vCFz7a",
        "outputId": "68e587c4-cd34-40e0-d9fd-4d392fb105a0"
      },
      "execution_count": 45,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Accuracy score of the test data :  0.9790865384615385\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Making Predictive System"
      ],
      "metadata": {
        "id": "Il3Wk5ycGILM"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X_new = X_test[3]\n",
        "\n",
        "prediction = model.predict(X_new)\n",
        "print(prediction)\n",
        "\n",
        "if prediction[0] ==0 :\n",
        "  print('The News is Real')\n",
        "else:\n",
        "  print('The News is Fake')\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kw0dAKhAGMCw",
        "outputId": "f0a4cde6-ca67-4125-a3b5-b629143c39dd"
      },
      "execution_count": 48,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[0]\n",
            "The News is Real\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(Y_test[3])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bfcMO9upGpXb",
        "outputId": "e59cf30c-b5f9-4722-b912-8d6ee806ff4c"
      },
      "execution_count": 49,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "0\n"
          ]
        }
      ]
    }
  ]
}